package com.crm.system.service.impl.trainingManager;

import com.crm.common.exception.FastDFSException;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactInformation;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.internalTrainingManager.CustomerUser;
import com.crm.model.entity.sys.FileUpload;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.trainingManager.Training;
import com.crm.model.entity.trainingManager.TrainingContact;
import com.crm.model.entity.trainingManager.TrainingDemand;
import com.crm.model.entity.trainingManager.TrainingDemandItem;
import com.crm.model.qo.trainingManager.AddTrainingQO;
import com.crm.model.qo.trainingManager.TrainingFormQO;
import com.crm.model.qo.trainingManager.TrainingQO;
import com.crm.model.vo.contactManager.ContactInformationVO;
import com.crm.model.vo.trainingManager.TrainingDetailVO;
import com.crm.model.vo.trainingManager.TrainingFormVO;
import com.crm.model.vo.trainingManager.TrainingVO;
import com.crm.service.contactManager.ContactDbService;
import com.crm.service.contactManager.ContactInformationDbService;
import com.crm.service.customerManager.CustomerContactPersonDbService;
import com.crm.service.customerManager.CustomerPoolDbService;
import com.crm.service.internalTrainingManager.CustomerUserDbService;
import com.crm.service.trainingManager.TrainingDbService;
import com.crm.system.service.FileUploadService;
import com.crm.system.service.contactManager.ContactService;
import com.crm.system.service.customerManager.CustomerPoolService;
import com.crm.system.service.trainingManager.TrainingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * bj
 * 联系人管理信息
 */
@Service
public class TrainingServiceImpl implements TrainingService {

    @Autowired
    private ContactDbService contactDbService;

    @Autowired
    private TrainingDbService trainingDbService;

    @Autowired
    private CustomerPoolDbService customerPoolDbService;

    @Autowired
    private CustomerContactPersonDbService customerContactPersonDbService;

    @Autowired
    private ContactService contactService;

    @Autowired
    private ContactInformationDbService contactInformationDbService;

    @Autowired
    private CustomerPoolService customerPoolService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    CustomerUserDbService customerUserDbService;

    /**
     * 用户模块 -- 内训管理列表
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingQO 查询条件
     * @return
     */
    @Override
    public DhPageInfo<TrainingVO> selectTrainingList(Integer pageIndex, Integer pageSize, TrainingQO trainingQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());

        //增加删除状态查询条件(0:正常;1:已删除)
        trainingQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(trainingQO.getDateType())) {
            if ("lastMonth".equals(trainingQO.getDateType())) {
                //上月
                dateUnit = 1;
            }
            if ("thisMonth".equals(trainingQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
        }

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("nt.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        Integer power = 0;
        List<TrainingVO> contactList = new ArrayList<TrainingVO>();
        if ("0".equals(trainingQO.getBs())) {
            /*if (rs) {
                //超级管理员
                trainingQO.setPower(0);
                contactList = trainingDbService.selectTrainingList(trainingQO, dateUnit, order.toString());
            } else {
                //普通管理员
                trainingQO.setPower(1);
                trainingQO.setCompanyId(userAccount.getCompanyId());
                contactList = trainingDbService.selectTrainingList(trainingQO, dateUnit, order.toString());
            }*/
            trainingQO.setPower(1);
            trainingQO.setAccountId(userAccount.getAccountId());
            contactList = trainingDbService.selectTrainingList(trainingQO, dateUnit, order.toString());
        } else {
            //普通用户
            trainingQO.setPower(2);
            trainingQO.setAccountId(userAccount.getAccountId());
            contactList = trainingDbService.selectTrainingList(trainingQO, dateUnit, order.toString());
        }


        PageInfo<TrainingVO> pageInfo = new PageInfo<TrainingVO>(contactList);
        return new DhPageInfo<TrainingVO>(pageInfo, contactList);
    }

    /**
     * 内训管理-新增内训
     * @param training 新增内训实体
     * @return
     */
    @Override
    public String addTraining(AddTrainingQO training) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<Training> list = trainingDbService.trainingList(userAccount.getAccountId());
        if(list.size() == 10) {
            return "NX0001";
        }

        Training tra = new Training();

        CustomerContactPerson customerContactPersons = null;
        //根据客户id、联系人名称和联系方式判重，是否插入客户联系人
        customerContactPersons = customerContactPersonDbService.isInsertCustomerContactPerson(
                training.getCustomerId(), training.getContactName(), training.getContactInformation());
        if (customerContactPersons == null) {
            //新增客户下的联系人
            customerContactPersons = getCustomerContactPerson(training, userAccount);
            Contact contact = getContact(training, userAccount);
            contactDbService.addContact(contact);
            tra.setContactId(contact.getContactId());
            List<ContactInformationVO> contactInformationList = contactService.getCustomerIdAndContactInformation(training.getCustomerId(), training.getContactInformation(), "");
            if (contactInformationList == null || contactInformationList.size() <= 0) {
                //新增联系方式
                ContactInformation contactInformationPOJO = getContactInformation(userAccount, training.getContactInformation(), training.getCustomerId(), contact);
                contactInformationDbService.addContactInformation(contactInformationPOJO);
            }
        }else{
            tra.setContactId(training.getContactId());
        }

        tra.setTrainingId(CommonUtils.getGUID());
        tra.setBusinessType(training.getBusinessType());
        tra.setContactPosition(training.getContactPosition());
        tra.setTrainingState(2);
        tra.setRebateStatus(0);
        tra.setBusinessBasicInformation(training.getBusinessBasicInformation());
        tra.setBusinessTrainingNeed(training.getBusinessTrainingNeed());
        tra.setEndTm(training.getEndTm());
        tra.setCreatePeopleId(userAccount.getAccountId());
        tra.setCreateTm(new Date());
        tra.setUpdatePeopleId(userAccount.getAccountId());
        tra.setUpdateTm(new Date());
        tra.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        tra.setCustomerId(training.getCustomerId());
        tra.setAccountId(userAccount.getAccountId());
        tra.setCompanyId(userAccount.getCompanyId());
        Boolean result = trainingDbService.addTraining(tra);
        if(result){
            return "SUCCESS";
        }else{
            return "FALSE";
        }
    }

    /**
     * 客户联系人记录
     * @Author pym
     * @Date  2021/3/4 14:22
     * @param training --跟进参数
     * @param userAccount  --账号信息
     * @return {@link {com.crm.model.entity.customerManager.CustomerContactPerson}}
     */
    private CustomerContactPerson getCustomerContactPerson(AddTrainingQO training, UserAccount userAccount) {
        CustomerContactPerson customerContactPerson = new CustomerContactPerson();
        customerContactPerson.setCustomerContactPersonId(CommonUtils.getGUID());
        customerContactPerson.setContactName(training.getContactName());
        customerContactPerson.setContactInformation(training.getContactInformation());
        customerContactPerson.setFollowUpTimes(0);
        customerContactPerson.setCreatePeopleId(userAccount.getAccountId());
        customerContactPerson.setCreateTm(new Date());
        customerContactPerson.setUpdatePeopleId(userAccount.getAccountId());
        customerContactPerson.setUpdateTm(new Date());
        customerContactPerson.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerContactPerson.setAbolishState(1);
        //customerContactPerson.setContactState();
        customerContactPerson.setMergeState(0);
        customerContactPerson.setCustomerId(training.getCustomerId());
        //新增客户下的联系人
        customerPoolDbService.addCustomerContactPerson(customerContactPerson);
        return customerContactPerson;
    }

    /**
     * 联系人管理表记录
     * @Author pym
     * @Date  2021/3/4 14:33
     * @param training --跟进参数
     * @param userAccount  --账号信息
     * @return {@link {com.crm.model.entity.contactManager.Contact}}
     */
    private Contact getContact(AddTrainingQO training, UserAccount userAccount) {
        Contact contact = new Contact();
        contact.setContactId(CommonUtils.getGUID());
        contact.setContactName(training.getContactName());
        contact.setContactInformation(training.getContactInformation());
        contact.setFollowUpTimes(0);
        contact.setCreatePeopleId(userAccount.getAccountId());
        contact.setCreateTm(new Date());
        contact.setUpdatePeopleId(userAccount.getAccountId());
        contact.setUpdateTm(new Date());
        contact.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //contact.setContactState();
        //contact.setMemberGrade();
        //contact.setMergeState(0);
        contact.setContactNature(0);
        contact.setAbolishState(1);
        contact.setCustomerId(training.getCustomerId());
        contact.setAccountId(userAccount.getAccountId());
        contact.setCompanyId(userAccount.getCompanyId());
        return contact;
    }

    /**
     * 联系方式记录
     * @Author pym
     * @Date  2021/3/11 8:48
     * @param userAccount --账号信息
     * @param contactInformation --联系方式
     * @param customerId --客户id
     * @param contact --联系人管理表
     * @return {@link {com.crm.model.entity.contactManager.ContactInformation}}
     */
    private ContactInformation getContactInformation(UserAccount userAccount, String contactInformation, String customerId, Contact contact) {
        ContactInformation contactInformationPOJO = new ContactInformation();
        contactInformationPOJO.setContactInformationId(CommonUtils.getGUID());
        contactInformationPOJO.setContactInformation(contactInformation);
        contactInformationPOJO.setContactType("跟进电话");
        contactInformationPOJO.setContactInformationType(0);
        contactInformationPOJO.setCreatePeopleId(userAccount.getAccountId());
        contactInformationPOJO.setCreateTm(new Date());
        contactInformationPOJO.setUpdatePeopleId(userAccount.getAccountId());
        contactInformationPOJO.setUpdateTm(new Date());
        contactInformationPOJO.setContactId(contact.getContactId());
        contactInformationPOJO.setCustomerId(customerId);
        contactInformationPOJO.setAbolishState(1);  //废止状态  1启动 2废止
        contactInformationPOJO.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return contactInformationPOJO;
    }

    /**
     * 内训详情-通过内训id查询内训详情
     * @param trainingId 内训id
     * @return
     */
    @Override
    public TrainingVO selectByTrainingId(String trainingId) {
        return trainingDbService.selectByTrainingId(trainingId);
    }

    /**
     * 内训管理-新增内训
     * @param trainingDemand 新增内训需求实体
     * @return
     */
    @Override
    public Boolean addTrainingDemand(TrainingDemand trainingDemand) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Boolean result = false;
                trainingDemand.setUpdatePeopleId(userAccount.getAccountId());
        trainingDemand.setUpdateTm(new Date());
        if(!StringUtils.isBlank(trainingDemand.getTrainingDemandId())) {
            result = trainingDbService.updateTrainingDemand(trainingDemand);
        }else{
            trainingDemand.setTrainingDemandId(CommonUtils.getGUID());
            trainingDemand.setCreatePeopleId(userAccount.getAccountId());
            trainingDemand.setCreateTm(new Date());
            trainingDemand.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            result = trainingDbService.addTrainingDemand(trainingDemand);
        }
        return result;
    }

    /**
     * 用户模块 -- 内训管理信息
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public TrainingDemand selectTrainingDemand(String trainingId) {
        return trainingDbService.selectTrainingDemand(trainingId);
    }

    /**
     * 内训管理-新增内训需求项
     * @param trainingDemandItem 新增内训需求项实体
     * @return
     */
    @Override
    public String addTrainingDemandItem(TrainingDemandItem trainingDemandItem) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<TrainingDemandItem> list = trainingDbService.selectTrainingDemandItemList(trainingDemandItem.getTrainingId(), "create_tm desc");
        if(list.size() == 10) {
            return "NXI0001";
        }
        trainingDemandItem.setTrainingDemandItemId(CommonUtils.getGUID());
        trainingDemandItem.setAddPeople(userAccount.getAccountId());
        trainingDemandItem.setAddPeopleName(userAccount.getUserName());
        trainingDemandItem.setCreatePeopleId(userAccount.getAccountId());
        trainingDemandItem.setCreateTm(new Date());
        trainingDemandItem.setUpdatePeopleId(userAccount.getAccountId());
        trainingDemandItem.setUpdateTm(new Date());
        trainingDemandItem.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        Boolean result =  trainingDbService.addTrainingDemandItem(trainingDemandItem);
        if(result){
            return "SUCCESS";
        }else{
            return "FALSE";
        }
    }

    /**
     * 用户模块 -- 内训管理项列表
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public DhPageInfo<TrainingDemandItem> selectTrainingDemandItemList(Integer pageIndex, Integer pageSize, String trainingId, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }


        List<TrainingDemandItem> list = trainingDbService.selectTrainingDemandItemList(trainingId, order.toString());



        PageInfo<TrainingDemandItem> pageInfo = new PageInfo<TrainingDemandItem>(list);
        return new DhPageInfo<TrainingDemandItem>(pageInfo, list);
    }

    /**
     * 用户模块 -- 内训联系人信息
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public DhPageInfo<TrainingContact> selectTrainingContactList(Integer pageIndex, Integer pageSize, String trainingId, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }


        List<TrainingContact> list = trainingDbService.selectTrainingContactList(trainingId, order.toString());



        PageInfo<TrainingContact> pageInfo = new PageInfo<TrainingContact>(list);
        return new DhPageInfo<TrainingContact>(pageInfo, list);
    }

    /**
     * 内训管理-内训详情-新增联系人到内训联系人下
     * @param trainingContact 商机联系人实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addTrainingContact(TrainingContact trainingContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        if(1 == trainingContact.getContactState()){
            //查询同一个内训下联系人只能有一个首要联系人   需要内训id和联系类型
            List<TrainingContact> list = trainingDbService.getTrainingAndContactState(trainingContact.getTrainingId(),trainingContact.getContactState());
            if(list != null && list.size() > 0){
                TrainingContact contact = list.get(0);
                //设置为次要联系人
                contact.setContactState(2);
                trainingDbService.updateTrainingContact(contact);
            }
        }
        String trainingContactId = CommonUtils.getGUID();
        trainingContact.setTrainingContactId(trainingContactId);
        trainingContact.setCreatePeopleId(userAccount.getAccountId());
        trainingContact.setCreateTm(new Date());
        trainingContact.setUpdatePeopleId(userAccount.getAccountId());
        trainingContact.setUpdateTm(new Date());
        trainingContact.setAbolishState(1); //废止状态 启动
        trainingContact.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        Boolean result = trainingDbService.addTrainingContact(trainingContact);
        return result;
    }

    /**
     *内训详情-内训联系人 开启/废止联系人
     * @param trainingContact 废止内训联系人实体
     * @return
     */
    @Override
    public Boolean updateAbolishStateById(TrainingContact trainingContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        trainingContact.setUpdatePeopleId(userAccount.getAccountId());
        trainingContact.setUpdateTm(new Date());
        Boolean result = trainingDbService.updateTrainingContact(trainingContact);
        return result;
    }

    /**
     * 用户模块 -- 内训详情 -- 同一内训线索下内训联系人的联系方式不能相同
     * @param trainingId 内训线索id
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public TrainingContact getTrainingIdAndContactInformation(String trainingId, String contactInformation) {
        return trainingDbService.getTrainingIdAndContactInformation(trainingId,contactInformation);
    }

    /**
     * 内训管理-内训详情-修改联系人到内训联系人下
     * @param trainingContact 内训联系人实体
     * @return
     */
    @Override
    public Boolean updateTrainingContact(TrainingContact trainingContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        trainingContact.setUpdatePeopleId(userAccount.getAccountId());
        trainingContact.setUpdateTm(new Date());
        Boolean result = trainingDbService.updateTrainingContact(trainingContact);
        return result;
    }

    /**
     * 用户模块 -- 内训详情 -- 修改联系人回显
     * @param trainingContactId 联系人id
     * @return
     */
    @Override
    public TrainingContact getTrainingContact(String trainingContactId) {
        return trainingDbService.getTrainingContact(trainingContactId);
    }

    /**
     * 管理模块--内训线索池列表
     * @param pageIndex/pageSize 页码/分页大小
     * @param trainingFormQO     查询条件
     * @param sortName/sortOrder 排序列名/排序规则(ASC/DESC)
     * @author yincuimei
     * @return
     */
    @Override
    public DhPageInfo<TrainingFormVO> selectTrainingFormVOList(Integer pageIndex, Integer pageSize, TrainingFormQO trainingFormQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        trainingFormQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());//删除状态

        Integer dateUnit = 0;

        if (!StringUtils.isBlank(trainingFormQO.getDateType())) {
            if ("thisWeek".equals(trainingFormQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(trainingFormQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("lastMonth".equals(trainingFormQO.getDateType())) {
                //上月
                dateUnit = 3;
            }
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }

        List<TrainingFormVO> trainingFormVOList = trainingDbService.selectTrainingFormVOList(trainingFormQO,dateUnit,order.toString());
        PageInfo<TrainingFormVO> pageInfo = new PageInfo<TrainingFormVO>(trainingFormVOList);
        return new DhPageInfo<TrainingFormVO>(pageInfo, trainingFormVOList);
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情
     * @param trainingId  --内训ID
     * @author yincuimei
     * @return
     */
    @Override
    public TrainingDetailVO selectTrainingDetailVOById(String trainingId) {
        return trainingDbService.selectTrainingDetailVOById(trainingId);
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑内训
     * @param training
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean updateTrainingById(Training training) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        training.setUpdatePeopleId(userAccount.getAccountId());//更新人
        training.setUpdateTm(new Date());//更新时间
        return trainingDbService.updateTrainingById(training);
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑客户图片
     * @param customer
     * @author yincuimei
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateCustomerImg(Customer customer,MultipartFile multipartFile) throws FastDFSException {
        if(multipartFile != null ){
            FileUpload fileUpload = fileUploadService.uploadFile(multipartFile, false);
            if(fileUpload != null){
                customer.setUploadId(fileUpload.getUploadId());
                Boolean result = customerPoolService.updateCustomerImgAndTag(customer);
                return result;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public String distribute(String trainingId, String userId) {
        //1. 通过线索id查询线索信息，获取客户id
        Training training = trainingDbService.selectByPrimaryKey(trainingId);
        String customerId = training.getCustomerId();
        //2. 通过客户id查看该客户是否已被领取
        List<CustomerUser> customerUsers = customerUserDbService.selectByCustomerAndOwner(customerId, null);
        if(customerUsers.size() > 0){
            //该客户已被领取
            //2.1 更新线索training_charge_id为该客户所有人
            training.setTrainingChargeId(customerUsers.get(0).getOwnerId());
            return trainingDbService.updateTrainingById(training) ? "000000" : "-1";
        }else{
            //2.2 更新线索training_charge_id及客户所有人为指定用户
            //2.2.1 更新客户所有人为指定用户
            //判断指定账号是否超出200
            Integer integer = customerUserDbService.selectCountByOwner(userId);
            if(integer >= 200){
                return "I00001"; //超出200客户上限，无法领取
            }

            //插入数据
            CustomerUser customerUser = new CustomerUser();
            customerUser.setCustomerId(customerId);
            customerUser.setOwnerId(userId);
            customerUser.setCreatePeopleId(userId);
            customerUser.setCreateTm(new Date());
            customerUser.setDeleteStatus((short)0);
            customerUser.setCustomerUserId(CommonUtils.getGUID());
            if(customerUserDbService.insertSelective(customerUser)){
                training.setTrainingChargeId(userId);
                return trainingDbService.updateTrainingById(training) ? "000000" : "-1";
            }else{
                return "-1";
            }
        }

    }
}
